package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.api.im.query.RecommendFriendQuery;
import com.liveinstars.im.domain.dao.model.RecommendFriend;
import com.liveinstars.im.domain.manager.RecommendFriendManager;
import com.liveinstars.im.domain.dao.mapper.RecommendFriendMapper;
import com.onepiece.shipelves.common.constant.Constant;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: zhouze
 * @date: 2020/3/3
 */
@Service("recommendFriendManager")
public class RecommendFriendManagerImpl extends ServiceImpl<RecommendFriendMapper, RecommendFriend> implements RecommendFriendManager {

    private final static Logger logger = LoggerFactory.getLogger(RecommendFriendManagerImpl.class);

    @Resource
    private RecommendFriendMapper recommendFriendMapper;

    @Override
    public void add(RecommendFriend recommendFriend) {
        try {
            recommendFriend.setState(Constant.STATE_VALID);
            recommendFriend.setSort(0);
            this.save(recommendFriend);
        }catch (Exception e) {
            logger.error("添加推荐好友异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void deleteById(Long id) {
        try {
            this.removeById(id);
        } catch (Exception e) {
            logger.error("删除推荐好友异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void updateSortById(Long id, Integer sort) {
        try {
            RecommendFriend recommendFriend = new RecommendFriend();
            recommendFriend.setId(id);
            recommendFriend.setSort(sort);
            this.updateById(recommendFriend);
        } catch (Exception e) {
            logger.error("更新推荐好友排序异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public List<RecommendFriend> listByQuery(RecommendFriendQuery query) {
        try {
            QueryWrapper<RecommendFriend> queryWrapper = new QueryWrapper<>();
            if (query.getId() != null) {
                queryWrapper.eq("id", query.getId());
            }
            queryWrapper.eq("state", Constant.STATE_VALID);
            List<RecommendFriend> list = this.list(queryWrapper.orderBy(true, true, "sort"));
            return list;
        } catch (Exception e) {
            logger.error("获取推荐好友列表异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
